package com.brightworks.langcollabplayer.commands
{
    import com.brightworks.brightcairngorm.utils.SQLQueue;
    import com.brightworks.langcollabplayer.business.LocalDB_CreateModuleDelegate;
    import com.brightworks.langcollabplayer.model.ModelLocator;
    import com.brightworks.langcollab.vo.ModuleVersionVO;

    import com.adobe.cairngorm.control.CairngormEvent;
    import com.universalmind.cairngorm.commands.Command;

    import mx.collections.ArrayCollection;
    import mx.utils.StringUtil;
    import mx.rpc.IResponder;

    public class UpdateLocalDBFromModelCommand extends Command
    {
        private var _sqlQueue:SQLQueue;

        public function UpdateLocalDBFromModelCommand()
        {
            super();
            _sqlQueue = new SQLQueue(result, fault);
        }

        override public function execute(event:CairngormEvent):void
        {
            super.execute(event);
            ///deleteModuleTable();
            ///createModuleTable();
            ///populateModuleTable();
            populateUserTable();
            _sqlQueue.execute();
        }

        override public function result(resultData:Object):void
        {
            // Should we set a status indicator in the model?
            var x:int = 0;
        }

        override public function fault(faultData:Object):void
        {
            var x:int = 0;
        }

        private function createModuleTable():void
        {
            var sql:String =
                "CREATE TABLE module (" +
                "    module_id INTEGER PRIMARY KEY, " +
                "    name TEXT, " +
                "    category TEXT, " +
                "    level TEXT" +
                ")";
            _sqlQueue.addItem(LocalDB_CreateModuleDelegate, sql);
        }

        private function deleteModuleTable():void
        {
            var sql:String = "DROP TABLE module"
            _sqlQueue.addItem(LocalDB_CreateModuleDelegate, sql);
        }

        private function populateModuleTable():void
        {
            /* var moduleData:ArrayCollection = ModelLocator.getInstance().downloadedModuleData;
            for each (var moduleVO:ModuleVO in moduleData)
            {
                //var sql:String =
                var sql:String = StringUtil.substitute("INSERT INTO module VALUES('{0}', '{1}', '{2}', '{3}')", moduleVO.module_id, moduleVO.name, moduleVO.category, moduleVO.level);
                _sqlQueue.addItem(LocalDB_CreateModuleDelegate, sql);
            } */
        }

        private function populateUserTable():void
        {
            var sql:String = "INSERT INTO user VALUES ('1','Douglas','McCarroll','douglasmccarroll','douglas@languagecollaborative.com','m','1951-10-16','0',NULL,'0','0')"
            _sqlQueue.addItem(LocalDB_CreateModuleDelegate, sql);
        }
    }
}
